const { getCustomList } = require('../../api/mock');
const RootApi =require('@/utils/RootApi')

RootApi.Page({
  data: {
    tabList: [
      {
        label: '全部',
        filterParams: {
          establishStatus: null, //建联状态 1,企微建联；2,未建联
          leaveInfoStatus: null, //留资状态 留资状态 1,已留资
          sortType: 'desc', //排序
        },
      },
      {
        label: '企微建联',
        filterParams: {
          establishStatus: 1,
          leaveInfoStatus: null,
          sortType: 'desc',
        },
      },
      {
        label: '未建联',
        filterParams: {
          establishStatus: 2,
          leaveInfoStatus: null,
          sortType: 'desc',
        },
      },
      {
        label: '已留资',
        filterParams: {
          establishStatus: null,
          leaveInfoStatus: 1,
          sortType: 'desc',
        },
      },
    ],

    curTabItem: '全部',

    filterParams: {
      establishStatus: null, //建联状态 0,未建连  1,已建联
      leaveInfoStatus: null, //留资状态 0,未留资  1,已留资
      sortType: 'asc', //排序类型：asc,升序 desc，降序
    },

    customerList: [],

    total: 0,

    refresherTriggered: false,

    scrollTop: 0,
  },
  customData: {
    pagination: {
      pageSize: 5,
      pageIndex: 1,
    },
  },
  onLoad(){
    this.customList(1);
  },
  onPullScrollDownRefresh() {
    this.resetPageIndex();
    
    this.customList(this.customData.pagination.pageIndex).finally(() => {
      setTimeout(() => {
        this.setData({
          refresherTriggered: false,
        });
      }, 500);
    });
  },
  reachScrollBottom() {
    if (this.data.customerList.length < this.data.total) {
      // 页码+1
      this.customData.pagination.pageIndex = this.customData.pagination.pageIndex + 1;

      this.customList(this.customData.pagination.pageIndex, true);
    }
    // else {
    // wx.showToast({
    //   title: '已经到底啦~',
    //   icon: 'none',
    // });
    // }
  },
  //点击筛选项
  handleActiveTabItem(e) {
    this.setData({
      curTabItem: e.detail.label,
      filterParams: e.detail.filterParams,
    });
    this.resetPageIndex();
    this.customList(this.customData.pagination.pageIndex);
  },
  //点击切换排序
  handleSort() {
    const sortType = this.data.filterParams.sortType === 'asc' ? 'desc' : 'asc';
    this.setData({
      'filterParams.sortType': sortType,
    });
    this.resetPageIndex();
    this.customList(this.customData.pagination.pageIndex);
  },
  // 重置当期页码
  resetPageIndex() {
    this.customData.pagination.pageIndex = 1;
    this.setData({
      scrollTop: 0,
    });
  },
  /**
   * 潜客列表
   * @param {*} pageIndex 页数（从1开始）
   * @param {*} isAppend 是否追加列表数据
   */
  async customList(pageIndex, isAppend = false) {
    try {
      let res = await getCustomList({
        ...this.data.filterParams, //过滤选项包括 establishStatus、leaveInfoStatus、sortType
        pageIndex,
        pageSize: this.customData.pagination.pageSize,
      });

      this.setData({
        total: res?.data?.total || 0,
      });

      if (res?.status === 1 && res?.data?.records) {
        let customerList = [];
        if (isAppend) {
          customerList = [...this.data.customerList, ...res.data.records];
        } else {
          customerList = res.data.records;
        }

        this.setData({
          customerList,
        });
      }
    } catch (err) {
      console.log('customList err', err);
    }
  },
  navToDetail() {
    wx.navigateTo({
      url: '/subPackages/customerDetail/pages/customerDetail/customerDetail',
    });
  },
});



